home *** CD-ROM | disk | FTP | other *** search
/ Total Web Page (Professional Suite) / Total Web Page 99.iso / CGI / DOWNLOAD.CGI-S=RAND_TEXT&C=TXT&F=RAND_TEXT.PL < prev    next >
Perl Script  |  1996-06-03  |  4KB  |  108 lines

  1. #!/usr/local/bin/perl
  2. ##############################################################################
  3. # Random Text                   Version 1.0                                  #
  4. # Copyright 1996 Matt Wright    mattw@worldwidemart.com                      #
  5. # Created 7/13/96               Last Modified 7/13/96                        #
  6. # Scripts Archive at:           http://www.worldwidemart.com/scripts/        #
  7. ##############################################################################
  8. # COPYRIGHT NOTICE                                                           #
  9. # Copyright 1996 Matthew M. Wright  All Rights Reserved.                     #
  10. #                                                                            #
  11. # Random Text may be used and modified free of charge by anyone so long as   #
  12. # this copyright notice and the comments above remain intact.  By using this #
  13. # code you agree to indemnify Matthew M. Wright from any liability that      #  
  14. # might arise from it's use.                                                 #  
  15. #                                                                            #
  16. # Selling the code for this program without prior written consent is         #
  17. # expressly forbidden.  In other words, please ask first before you try and  #
  18. # make money off of my program.                                              #
  19. #                                                                            #
  20. # Obtain permission before redistributing this software over the Internet or #
  21. # in any other medium.  In all cases copyright and header must remain intact #
  22. ##############################################################################
  23. # Define Variables                                                           #
  24.  
  25. # This is the file in which all of your random text phrases are stored.      #
  26.  
  27. $random_file = "/path/to/random.txt";
  28.  
  29. # The delimiter specifies how each phrase is distinguished from another.  For#
  30. # instance, the common fortune file (a Unix program) is delimited by a new   #
  31. # line followed by two % signs on the next line and then a new line.  This is#
  32. # a pretty good format and you can read more about it in the README file.    #
  33.  
  34. $delimiter = "\n\%\%\n";
  35.  
  36. # Done                                                                      #
  37. ##############################################################################
  38.  
  39. # Open the file containing phrases and read it in.
  40.  
  41. open(FILE,"$random_file") || &error('open->random_file',$random_file);
  42. @FILE = <FILE>;
  43. close(FILE);
  44.  
  45. # Join these lines from the file into one large string.
  46. $phrases = join('',@FILE);
  47.  
  48. # Now split the large string according to the $delimiter.
  49. @phrases = split(/$delimiter/,$phrases);
  50.  
  51. # Invoke srand; with a seed of the time and pid.  If you are on a machine
  52. # which doesn't put the pid into $$ (ie. Macintosh, Win NT, etc...), change
  53. # this line to:  srand(time ^ 22/7);
  54.  
  55. srand(time ^ $$);
  56.  
  57. # Now pluck our random phrase out of the @phrases array!  But wait!  This
  58. # only returns a number.
  59.  
  60. $phrase = rand(@phrases);
  61.  
  62. # Print out the Content-type header, so the browser knows what's going on.
  63.  
  64. print "Content-type: text/html\n\n";
  65.  
  66. # Change this number into the text we want to return and print it!
  67.  
  68. print $phrases[$phrase];
  69.  
  70. # All Done!
  71.  
  72. exit;
  73.  
  74. # Was there an error?  If so, let's report that sucker so it can get fixed!
  75.  
  76. sub error {
  77.     ($error,$file) = @_;
  78.     print <<"END_ERROR";
  79. Content-type: text/html
  80.  
  81. <html>
  82.  <head>
  83.   <title>ERROR: Random File Unopenable</title>
  84.  </head>
  85.  <body bgcolor=#FFFFFF text=#000000>
  86.   <center>
  87.    <h1>ERROR: Random File Unopenable</h1>
  88.   </center>
  89.  
  90. The random file, as specified in the \$random_file perl variable was 
  91. unopenable.<p>
  92. END_ERROR
  93.  
  94.     if (-e $random_file) {
  95.         print "The file was found on your system, so make sure that it is\n";
  96.         print "readable by the web server.  This means you will need to\n";
  97.         print "execute the following command:<pre>\n";
  98.         print "    chmod 744 $random_file\n";
  99.         print "</pre>\n";
  100.     }
  101.     else {
  102.         print "The file was not found on your file system.  This means that\n";
  103.         print "it has either not been created or the path you have specified\n";
  104.         print "in \$trrandom_file is incorrect.\n";
  105.     }
  106.     exit;
  107. }
  108.